package profileEditor;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import GUIMain.GUIFocusManager;

public class RuleEditorHelp {
	private static JDialog dialog;
	private static boolean initialized;
	private static JTextPane textpane;
	private static JScrollPane jsp;
	private static JButton back;
	private static GridBagConstraints gbc;
	
	public static void open(){
		if(!initialized){
			initialize();
		}
		GUIFocusManager.add(dialog);
	}
	private static void initialize(){
		dialog = new JDialog();
		dialog.setLayout(new GridBagLayout());
		dialog.setTitle("Rule Editor Help");
		textpane = new JTextPane();
		jsp = new JScrollPane(textpane);
		textpane.setEditable(false);
		back = new JButton("Back");
		gbc = new GridBagConstraints();
		
		StringBuilder s = new StringBuilder("");
		s.append("Welcome to the help section for rule editing" + "\n");
		s.append("\n");
		s.append("NOTE: The group number function is not in use at this time" + "\n");
		s.append("\n");
		s.append("To mark a field we must first setup a list of conditions to be met. " +
				"This is setup using a series of fields and equalities. " + "\n");
		s.append("\n");
		s.append("Subject Field - This is the first field to be compared to" + "\n");
		s.append("Value - This field serves several purposes and is dependant on the type of field selected in the subject.  In the case of a STRING(TEXT) type, any value entered in this field will be " +
				"directly compared to the subject field.  In the case of a DATE field, this value will specify the number of days " +
				"before or after the target field.  For either type you may enter NULL as the value, which will mark the field " +
				"if the cell is blank" + "\n");
		s.append("Equality - This field is only active when a DATE type field is selected and allows you to set how you want to use " +
				"the data in the value field in your comparison.  The two options available are BEFORE and AFTER.  The behavior of these " +
				"two selections differ in the following ways:  If BEFORE is selected, any date greater than the comparison interval" +
				"will be highlighted.  If AFTER is selected, any date less than the comparison interval is selected.  See examples below for clarification." + "\n");
		s.append("Target Field - This field is compared against the subject field using the conditions specified " +
				"in the value and equality fields." + "\n");
		s.append("Connector - Allows the user to enter multiple rules and lets them decide on how they should work in " +
				"conjunction" + "\n");
		s.append("\n");
		s.append("\n");
		s.append("Examples:"+"\n");
		s.append("\n");
		s.append("Mark if ATA is NULL" + "\n");
		s.append("This field will only be marked if ATA is null.  No other comparisons are made" + "\n");
		s.append("\n");
		s.append("Mark if ATA is NULL -AND- Mark if ETA is 2 BEFORE TODAY" + "\n");
		s.append("These rules will only mark the field if both conditions are met.  ATA field must be blank and " +
				"the date in the ETA field must be at least 2 days before today.  The following example " +
				"satisfies these rules - ATA is Blank, ETA " +
				"is 5/1/2011 and Today's date is 5/4/2011" + "\n");
		s.append("\n");
		s.append("Mark if ATA is NULL -OR- Mark if ETA is 2 BEFORE TODAY" + "\n");
		s.append("This is the same example as above, with the connector changed to -OR-. " +
				"Now if either condition is satisfied, the field will be marked. " +
				"The following satisfies these rules - ATA is not blank, ETA is 5/1/2011 and " +
				"Today's date is 5/4/2011." + "\n");
		s.append("\n");
		s.append("Mark if ATA is NULL -AND- Mark if ETA is 2 AFTER TODAY" + "\n");
		s.append("The difference from the above is the BEFORE has been switched for AFTER. " +
				"Now the following satisfies these rules - ATA is blank, ETA is 5/6/2011 and Today's date" +
				"is 5/4/2011.  Also note that the dates 5/6/2011, 5/5/2011 ... 1/1/2011, "+  "\n");
		textpane.setText(s.toString());
		gbc.insets.left = 0;
		gbc.insets.right = 0;
		gbc.insets.top = 2;
		gbc.insets.bottom = 2;
		
		gbc.gridwidth =01;
		gbc.gridheight = 1;
		gbc.gridx = 0;
		gbc.gridy = 0;
		dialog.add(jsp,gbc);
		gbc.gridy = 1;
		dialog.add(back,gbc);
		
		attachListeners();

		textpane.setCaretPosition(0);
		jsp.setPreferredSize(new Dimension(400,500));
		GUIFocusManager.setDialogProperties(dialog);
	}
	private static void attachListeners(){
		back.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				GUIFocusManager.remove();
			}
		});
	}
}
